package com.example.web.controller;

import com.example.web.entity.Container;
import com.example.web.entity.Goods;
import com.example.web.repository.ContainerRepository;
import com.example.web.repository.GoodRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/container")
@Slf4j
public class ContainerController {

    @Autowired
    private ContainerRepository containerRepository;

    @Autowired
    private GoodRepository goodRepository;


    @RequestMapping("/add")
    public ResponseEntity containerAdd(Container container) {
        containerRepository.saveAndFlush(container);
        return ResponseEntity.ok().build();
    }

    @RequestMapping("/bind/{containerId}/{goodsId}")
    public ResponseEntity event(@PathVariable("containerId")Long containerId, @PathVariable("goodsId") Long goodsId) {
        Container container = containerRepository.getOne(containerId);
        container.setGoods(goodRepository.getOne(goodsId));
        containerRepository.save(container);
        return ResponseEntity.ok().build();
    }


}
